﻿@{
    ViewBag.title = "学生信息编辑";
}
@section headerSection{
    <style>
        .form-group {
            margin-bottom: 8px;
        }
    </style>
}

<div class="panel-body autoHeight" style="width:700px;">
    <form id="editForm" class="form-horizontal m-t">
        <div class="form-group">
            <input type="hidden" id="id" name="id" value="@ViewBag.id" />
            <label class="col-sm-4 control-label">所属学校<span class="glyphicon glyphicon-star-empty" style="color:red;" aria-hidden="true"></span></label>
            <div class="col-sm-8">
                <select id="schoolName" name="schoolName" required aria-required="true" title="所属学校" class="form-control input-sm" style="padding-bottom:4px;padding-top:4px;">
                    <option></option>
                   @for (var i = 0; i < ViewBag.units.Count; i++) 
                   {
                    <option value="@ViewBag.units[i].text" code="@ViewBag.units[i].value">@ViewBag.units[i].text</option>
                   }
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">所属班级<span class="glyphicon glyphicon-star-empty" style="color:red;" aria-hidden="true"></span></label>
            <div class="col-sm-8">
                <select id="classId" name="classId" required aria-required="true" title="选择所属学校后加载班级" class="form-control input-sm" style="padding-bottom:4px;padding-top:4px;">
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">学生姓名<span class="glyphicon glyphicon-star-empty" style="color:red;" aria-hidden="true"></span></label>
            <div class="col-sm-8">
                <input name="studentName" type="text" placeholder="请输入学生姓名" title="请输入学生姓名"
                       autocomplete="off" class="form-control input-sm"  required aria-required="true"  />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">学号<span class="glyphicon glyphicon-star-empty" style="color:red;" aria-hidden="true"></span></label>
            <div class="col-sm-8">
                <input name="studentId" type="text" placeholder="请输入学号" title="请输入学号"
                       autocomplete="off" class="form-control input-sm" required aria-required="true"   />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">专业</label>
            <div class="col-sm-8">
                <input name="major" type="text" placeholder="请输入专业"
                       autocomplete="off" class="form-control input-sm" />
                
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">性别</label>
            <div class="col-sm-8">
                <select id="sex" name="sex" class="form-control input-sm" style="padding-bottom:4px;padding-top:4px;">
                    <option value="true">男</option>
                    <option value="false">女</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">电话</label>
            <div class="col-sm-8">
                <input name="tel" type="text" placeholder="请输入电话号码"
                       autocomplete="off" class="form-control input-sm" />
                
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">邮箱</label>
            <div class="col-sm-8">
                <input name="email" type="text" placeholder="请输入邮箱"
                       autocomplete="off" class="form-control input-sm" />
                
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">身份证</label>
            <div class="col-sm-8">
                <input name="idCode" type="text" placeholder="请输入身份证"
                       autocomplete="off" class="form-control input-sm" />
                
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">状态</label>
            <div class="col-sm-8">
                <select name="state" class="form-control input-sm" style="padding-bottom:4px;padding-top:4px;">
                    <option value="1">启用</option>
                    <option value="0">停用</option>
                </select>
            </div>
        </div>

        <div id="saveContent" class="form-group">
            <div class="col-sm-4 col-sm-offset-4">
                <button id="save" type="button" class="btn btn-w-m btn-primary">
                    保存
                </button>
            </div>
        </div>
    </form>
</div>

@section footSection{
    <script src="~/Resources/tools/jqueryExtend.js"></script>
    <script>
        var urlRoot = ctx + '/Student/';
        function loadClass(unit) {
            $.post(urlRoot + 'getSchoolClass', { school: unit }, function (server) {
                if (server.Statu == 0) {
                    loadClassOption(server.Data);
                } else {
                    layer.alert('加载班级列表失败', { icon: 5 });
                }
            });
        }

        function find() {
            var _id = $('#id').val();
            if (_id != "") {
                $.post(urlRoot + 'find', { id: _id }, function (server) {
                    if (server.Statu == 0) {
                        loadClassOption(server.Data.classList);

                        server.Data.sex = server.Data.sex ? "true" : "false";
                        $('#editForm').LoadForm(server.Data);
                    } else {
                        layer.alert('加载数据失败', { icon: 5 });
                    }
                });
            }
        }

        function loadClassOption(classList) {
            $('#classId').empty();
            for (var i in classList) {
                $('#classId').append($('<option value="' + classList[i].value + '" >' + classList[i].text + '</option>'));
            }
        }

        function save() {
            if ($('#editForm').valid()) {
                var PIndex;

                var code = $('#schoolName option:selected').attr('code');
                $('#schoolCode').val(code);

                var userId = $('#teacherName option:selected').attr('userId');
                $('#teacherUserId').val(userId);

                $('#editForm').ajaxSubmit({
                    url: urlRoot + 'save',
                    type: "post",
                    dataType: "json",
                    beforeSubmit: function (arr, $form, options) {
                        PIndex = layer.msg('正在提交数据', { icon: 16, shade: [0.3, '#393D49'], time: 0 });
                    },
                    success: function (result, status, xhr, $form) {
                        layer.close(PIndex);
                        if (result.Statu === 0) {
                            $('#id').val(result.Data);
                            layer.msg('保存成功', { icon: 1 });
                        } else if (result.Statu == 2) {
                            layer.alert(result.Msg, { icon: 7 });
                        }
                        else
                            layer.alert('保存失败', { icon: 5 });
                    },
                    error: function (xhr, status, error, $form) {
                        layer.close(PIndex);
                        layer.alert("保存失败", { icon: 5 });
                    }
                });
            }
        }

        $(function () {
            $('#schoolName').change(function () {
                loadClass($(this).val());
            });

            $('#save').click(function () {
                save();
            });

            find();
        });
    </script>
}
